package com.yy.permissions_general.module.system.roleAuth.controller;

import cn.hutool.core.convert.Convert;
import com.yy.permissions_general.module.system.aopLog.aop.MyLog;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
import com.yy.permissions_general.module.system.roleAuth.entity.RoleAuth;
import com.yy.permissions_general.module.system.roleAuth.service.RoleAuthService;
import com.yy.permissions_general.utils.ResultUtil;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


/**
 * <p>
 * 角色权限关联前端控制器
 * </p>
 *
 * @author YY
 * @since 2021-10-07
 */
@RestController
@RequestMapping("/roleAuth")
public class RoleAuthController {

    @Resource
    private RoleAuthService roleAuthService;

    /**
    * 查询列表
    * @param roleAuth
    * @param limit
    * @param page
    * @return
    */
    @RequestMapping("/list")
    @MyLog("查询角色权限关联信息")
    public ResultUtil pageList(RoleAuth roleAuth,@RequestParam(value = "limit",defaultValue = "999999")int limit, @RequestParam(value = "page",defaultValue = "0")int page) {
      return ResultUtil.buildPagination(roleAuthService.selectPageList(roleAuth,limit,page));
    }

    /**
    * 新增数据
    * @param map
    * @return
    */
    @RequestMapping(value = "/addRoleAuth",consumes = "application/json; charset=utf-8")
    @PreAuthorize("hasPermission('/roleAuth/addRoleAuth','roleAuth:addRoleAuth')")
    @MyLog("添加角色权限关联")
    public ResultUtil addRoleAuth(@RequestBody Map<String,Object> map) {
        roleAuthService.deleteByRoleId(Convert.toStr(map.get("roleId")));
        roleAuthService.saveBatch(Convert.toList(RoleAuth.class,map.get("roleAuthList")));
        return ResultUtil.success();
    }

    /**
    * 根据ID删除记录
    * @param id
    * @return
    */
    @RequestMapping("/delete")
    @PreAuthorize("hasPermission('/roleAuth/delete','roleAuth:delete')")
    @MyLog("删除角色权限关联")
    public ResultUtil delete(Integer id) {
        roleAuthService.deleteById(id);
        return ResultUtil.success();
    }

    /**
    * 批量删除数据
    * @param ids ID集合
    * @return
    */
    @RequestMapping("/deleteBatch")
    @PreAuthorize("hasPermission('/roleAuth/deleteBatch','roleAuth:deleteBatch')")
    @MyLog("批量删除角色权限关联")
    public ResultUtil deleteBatch(@RequestParam(value = "ids[]",required = true) List<Long> ids) {
        roleAuthService.deleteBatch(ids);
        return ResultUtil.success();
    }

    /**
     * 根据角色ID获取权限
     * @param roleId 角色ID
     * @return
     */
    @RequestMapping("/getRoleAuthTree")
    @MyLog("查询角色权限")
    public ResultUtil getRoleAuthTree(Integer roleId) {
        return ResultUtil.buildPagination(roleAuthService.getRoleAuthTree(roleId));
    }

}
